package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
/*import com.itheima.service.MenuService;*/
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference//查找服务
    private RoleService roleService;

    @RequestMapping("/findAll")
    //分页查询
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.pageQuery(queryPageBean);
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Role role) {
        try {
            role.setPermissionIds(role.getPermissionIds());
            role.setMenuIds(role.getMenuIds());
            roleService.add(role);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            return new Result(false, "添加失败,请稍后再试");
        }
    }
    @RequestMapping("/findMenuId")
    public Result findMenuId(Integer id){
        try {
            List<Integer> menuIdList = roleService.menuIdList(id);
            return new Result(true, "查询成功",menuIdList);
        } catch (Exception e) {
            return new Result(false, "服务器异常,请刷新重试");
        }
    }


    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Role role = roleService.findById(id);
            return new Result(true, "查询成功",role);
        } catch (Exception e) {
            return new Result(false, "服务器异常,请刷新重试");
        }
    }
    @RequestMapping("/findPermissionId")
    public Result findPermissionId(Integer id){
        try {
            List<Integer> permissionIdList = roleService.permissionIdList(id);
            return new Result(true, "查询成功",permissionIdList);
        } catch (Exception e) {
            return new Result(false, "服务器异常,请刷新重试");
        }
    }
    @RequestMapping("/findAllMenu")
    public Result findAll() {
        try {
            List<Menu> menuList = roleService.findAllMenu();
            return new Result(true, "查询成功",menuList);
        } catch (Exception e) {
            return new Result(false, "服务器异常,请刷新重试");
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role) {
        try{
            roleService.edit(role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "修改失败");//新增失败
        }
        return new Result(true,"修改成功");//新增成功
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            Result result = roleService.delete(id);
            return result;
        } catch (Exception e) {
            return new Result(false, "服务器异常,请刷新重试");
        }
    }
    /*public Result findAllPermission()*/



}
